package com.login.service.user;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.login.bean.Buyer;
import com.login.dao.impl.BuyerDaoImpl;
import com.login.dao.impl.ManagerDaoImpl;
import com.login.dao.impl.SalerDaoImpl;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	BuyerDaoImpl loginDaoImpl=new BuyerDaoImpl();
	SalerDaoImpl loginSaler=new SalerDaoImpl();
	ManagerDaoImpl loginManager=new ManagerDaoImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("UTF-8");
		String username=request.getParameter("name");
		String password=request.getParameter("pwd");
		String userType=request.getParameter("loginUser");
		request.getSession().setAttribute("userName", username);
		Buyer user=new Buyer();
		user.setName(username);
		user.setPwd(password);
		user.setIdentify(userType);
		request.getSession().setAttribute("user", user);
		System.out.println("login的是："+userType);
		boolean login=false;
			if(userType.equals("buyer")){
				login=loginDaoImpl.BuyerLogin(username, password);
				if(login){
				response.sendRedirect("BuyerIndex.jsp");
				}
				else{					
					//response.getWriter().println("密码错误");				
					response.sendRedirect("login.jsp");
				}
			}
			if(userType.equals("seller")){
				login=loginSaler.SalerLogin(username, password);
				if(login){
				response.sendRedirect("success.jsp");
				}
				else{
					//response.getWriter().println("密码错误");				
					response.sendRedirect("login.jsp");
				}
			}
			else if(userType.equals("manager")){
				login=loginManager.ManagerLogin(username, password);
				if(login){					
				response.sendRedirect("/groupProject/UserList");
				}else{
					response.sendRedirect("login.jsp");
				}
				//response.getWriter().println("<script type='text/javascript'>function aa(){location='/groupProject/UserList'} aa();</script>");
			}
		}
		
		


	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
